import request from "./request";
import {useSystemStore} from "@/store/system";
const $systemStore = useSystemStore();
const token = $systemStore.token;

const http = {
  get(url, params = {}) {
    const config = {
      method: "get",
      url: url,
      params
    };
    return new Promise((resolve,  reject) => {
      request(config).then((res) => {

        if (res) {
          resolve(res);
        }
      }).catch(err => {
        reject(err);
      });
    });
  },
  post(url, data = {}) {
    const config = {
      method: "post",
      url: url,
      data
    };

    return new Promise((resolve,  reject) => {
      request(config)
          .then((res) => {
        if (res) {
          resolve(res);
        }
      }).catch(err => {
        reject(err);
      });
    });
  },
  put(url, data = {}) {
    const config = {
      method: "put",
      url: url,
      data
    };

    return new Promise((resolve,  reject) => {
      request(config).then((res) => {
        if (res) {
          resolve(res);
        }
      });
    });
  },
  posts(url, params) {
    const config = {
      method: "post",
      url: url
    };
    if (params) config.params = params;
    return new Promise((resolve, reject) => {
      request(config).then((res) => {
        if (res) {
          resolve(res);
        }
      });
    });
  },
  // , "authorization": token
  // 未登录上传语音
  filePost(url, data = {}) {
    const config = {
      method: "post",
      url: url,
      responseType: "blob",
      headers: { "Content-Type": "application/json" },
      data
    };
    return new Promise((resolve) => {
      request(config).then((res) => {
        if (res) {
          resolve(res);
        }
      });
    });
  },
  // 登录上传语音
  loginFilePost(url, data = {}) {
    const config = {
      method: "post",
      url: url,
      responseType: "blob",
      headers: {
        "Content-Type": "application/json",
        "authorization": token

      },
      data
    };
    return new Promise((resolve) => {
      request(config).then((res) => {
        if (res) {
          resolve(res);
        }
      });
    });
  },
  patch(url, data = {}) {
    const config = {
      method: "patch",
      url: url,
      data
    };
    return new Promise((resolve) => {
      request(config).then((res) => {
        if (res) {
          resolve(res);
        }
      });
    });
  },
  delete(url) {
    const config = {
      method: "delete",
      url: url
    };
    return new Promise((resolve,  reject) => {
      request(config).then((res) => {
        if (res) {
          resolve(res);
        }
      }).catch(err => {
        reject(err);
      });
    });
  }
};
export default http;
